var vue = new Vue({
    el: '#cqr',
    data: {
        username: '',
        password: ''
    },
    methods: {
        login: function () {
            if (vue.username == '') {
                alert("用户名不能为空！")
                return;
            }
            if (vue.password == '') {
                alert("密码不能为空！");
                return;
            }
            if (vue.username.length > 16) {
                alert("请输入长度小于16的的用户名!")
                return;
            }
            if (vue.password.length > 16) {
                alert("请输入长度小于16的的密码!");
                return;
            }
            var data = {
                username: vue.username,
                password: vue.password
            };
            $.ajax({
                type: "POST",
                url: "user/login",
                data: data,
                dataType: "JSON",
                success: function (msg) {
                    if (msg == true) {
                        parent.location.href = '/tomatochat';
                    } else {
                        alert("登录失败");
                    }
                },
                error: function (mes) {
                    alert("error");
                }
            });
        },
        register: function () {
            window.location.href = 'register';
        },
        getRandAccount: function () {
            $.ajax({
                type: "POST",
                url: "user/getRandAccount",
                dataType: "JSON",
                success: function (msg) {
                    if(msg.state='success'){
                        vue.username=msg.username;
                        vue.password=msg.password;
                        vue.login();
                    }else{
                        alert(msg.message);
                    }
                },
                error: function (mes) {
                    alert("error");
                }
            });
        }
    }
})